
	package com.chinapost.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.po.Tdept;
import com.chinapost.po.Tjfsz;
import com.chinapost.po.Tkind;
import com.chinapost.po.Tuser;
import com.chinapost.service.JfszServiceI;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Jfsz;

@Service("jfszService")
public class JfszServiceImpl implements JfszServiceI {
	
	@Autowired
	private BaseDaoI<Tjfsz> jfszDao;
	@Autowired
	private BaseDaoI<Tdept> deptDao;
	@Autowired
	private BaseDaoI<Tuser> userDao;
	@Autowired
	private BaseDaoI<Tkind> kindDao;


	@Override
	public void add(Jfsz jfsz) {
		Tjfsz t = new Tjfsz();
		Date d = new Date();
		BeanUtils.copyProperties(jfsz, t);
		t.setCid(UUID.randomUUID().toString());
		t.setCcreatedate(new Timestamp(d.getTime()));
		t.setCmodifydate(new Timestamp(d.getTime()));
		t.setTdept(deptDao.get(Tdept.class, jfsz.getDeptid()));
		t.setTkind(kindDao.get(Tkind.class,jfsz.getKindid()));
		if (!StringUtils.isEmpty(jfsz.getCreateuserid())) {
			t.setTuserByCcreateuserid(userDao.get(Tuser.class, jfsz.getCreateuserid()));
			t.setTuserByCmodifyuserid(userDao.get(Tuser.class, jfsz.getCreateuserid()));
		}
		jfszDao.save(t);
	}

	@Override
	public void edit(Jfsz jfsz) {
		Tjfsz t = jfszDao.get(Tjfsz.class, jfsz.getCid());
		BeanUtils.copyProperties(jfsz, t,new String[]{"ccreatedate"});
		Date d = new Date();
		t.setCmodifydate(new Timestamp(d.getTime()));
		t.setTdept(deptDao.get(Tdept.class, jfsz.getDeptid()));
		t.setTkind(kindDao.get(Tkind.class,jfsz.getKindid()));
		if (!StringUtils.isEmpty(jfsz.getModifyuserid())) {
			t.setTuserByCmodifyuserid(userDao.get(Tuser.class, jfsz.getModifyuserid()));
		}
	}

	@Override
	public void delete(Jfsz jfsz) {
		String cids = "";
		if (jfsz.getIds() != null) {
			String ids = jfsz.getIds();
			for (String cid : ids.split(",")) {
				cids += "'" + cid + "'" + ",";
			}
			cids = cids.substring(0, cids.length() - 1);
			String hql = "delete Tjfsz t where t.cid in (" + cids + ")";
			jfszDao.executeHql(hql);
		}
	}

	@Override
	public DataGrid datagrid(Jfsz jfsz) {
		DataGrid dg = new DataGrid();
		dg.setRows(listRows(find(jfsz)));
		dg.setTotal(total(jfsz));
		return dg;
	}

	private Long total(Jfsz jfsz) {
		String hql = "select count(*) from Tjfsz t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(jfsz, hql, params);
		return jfszDao.count(hql, params);
	}

	private List<Tjfsz> find(Jfsz jfsz) {
		String hql = "from Tjfsz t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(jfsz, hql, params);
		if (jfsz.getOrder() != null && jfsz.getSort() != null) {
			hql += " order by " + jfsz.getSort() + " " + jfsz.getOrder();
		}
		return jfszDao.find(hql, params, jfsz.getPage(), jfsz.getRows());
	}

	private List<Jfsz> listRows(List<Tjfsz> lt) {
		List<Jfsz> rows = new ArrayList<Jfsz>();
		if (lt != null && lt.size() > 0) {
			for (Tjfsz t : lt) {
				Jfsz sub = new Jfsz();
				BeanUtils.copyProperties(t, sub);
				sub.setDeptid(t.getTdept().getCid());
				sub.setDeptname(t.getTdept().getCname());
				sub.setKindid(t.getTkind().getCid());
				sub.setKindname(t.getTkind().getCname());
				rows.add(sub);
			}
		}
		return rows;
	}

	private String addWhere(Jfsz jfsz, String hql, Map<String, Object> params) {
		if (jfsz != null && jfsz.getCid() != null && !jfsz.getCid().trim().equals("")) {
			hql += " and t.cid = :id ";
			params.put("id", jfsz.getCid());
		}
		if (jfsz != null && jfsz.getDeptIds() != null && jfsz.getDeptIds().size() > 0) {
			hql += " and t.tdept.cid in(:deptids)";
			params.put("deptids", jfsz.getDeptIds());
		}
		return hql;
	}

}

